--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

Use("common/Const");
Use("common/Log");
Use("common/Position");
Use("common/Creature");
Use("common/Skill");
Use("common/Task");

-- Bombard Task (advancing, aimed, single strike)
-- progress: progress state
-- id: who is responsible for the task
-- origin: where the task started
-- time: when the task started
-- hp: hit points at task creation
-- sp: skill points at task creation
-- resilience: modified resilience value
-- skill: skill necessary for the attack
-- destination: desired location to bombard
Bombard = {};

function Bombard.Create(_pet, _skill, _destination)
  local instance = Task.Create(_pet);

  instance.resilience = _pet:GetResilience();
  instance.skill = _skill;
  instance.destination = _destination;

  --[[ Attack one time ]]--
  function instance:Execute()
    Debug("Bombard.Execute()");

    if (GetV(V_HP, self.id) < (self.hp / self.resilience)) then
      -- We've taken a lot of damage since the task began. Abandon this
      -- task and try to do something else.
      Info("Bombard.Execute() interrupting task")
      return self:Progress(PROGRESS_FAILURE);
    end

    -- Try to move in range.
    local from = Position.Find(self.id);

    -- If it's too far away, just sit there.
    if (Position.Measure(from, self.destination) >= VISIBILITY) then
      Alert("Can't reach target. (" .. motion .. ")");
      return self:Progress(PROGRESS_FAILURE);
    end

    Debug("Bombard.Execute() I'm at " .. from.x .. "," .. from.y .. " and aiming at " .. self.destination.x .. "," .. self.destination.y);
    local range = self.skill:Estimate();
    local result = self:Approach(self.destination, range);
    if (result == PROGRESS_SUCCESS) then
      -- We should be in range now. Start the bombardment!
      return self:Progress(self:Apply(self.skill, self.destination));
    else
      -- We aren't in range. Report the movement result.
      return self:Progress(result);
    end
  end

  return instance;
end
